package com.meritdata.cloud.data.service.development.controller;

import com.meritdata.cloud.data.base.Page;
import com.meritdata.cloud.data.service.development.model.DataApi;
import com.meritdata.cloud.data.service.development.service.DataApiService;
import com.meritdata.cloud.resultmodel.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

@RequestMapping("/dataapi")
@Controller
public class DataApiController {
    @Autowired
    private DataApiService service;

    @RequestMapping("/page")
    @ResponseBody
    public ResultBody page(@RequestParam Map<String,Object> param) {
        ResultBody r = new ResultBody();
        Page<DataApi> page = service.page(param);
        r.setData(page);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/create")
    @ResponseBody
    public ResultBody create(@RequestParam String name ,@RequestParam String url ,
                             @RequestParam String type ,@RequestParam String description,
                             @RequestParam String categoryId ) {
        ResultBody r = new ResultBody();
        DataApi d = new DataApi();
        d.setName(name);
        d.setUrl(url);
        d.setType(type);
        d.setDescription(description);
        d.setCreateTime(new Date());
        d.setUpdateTime(new Date());
        d.setCategoryId(categoryId);
        d = service.save(d);
        r.setData(d);
        r.setSuccess(true);
        return r;
    }
    @RequestMapping("/update")
    @ResponseBody
    public ResultBody update(@RequestParam String id ,@RequestParam String name ,@RequestParam String url ,
                             @RequestParam String type ,@RequestParam String description ) {
        ResultBody r = new ResultBody();
        r.setData(service.update(id,name,type,url,description,new Date()));
        r.setSuccess(true);
        return r;
    }
}
